package com.fare.taluo.web.action;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.inject.Inject;

import net.sf.json.JSONArray;
import net.sf.json.JsonConfig;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.stereotype.Controller;

import com.fare.taluo.BaseFdcAction;
import com.fare.taluo.application.crawl.crawlstatistics.PersistentRecordeApplication;
import com.fare.taluo.base.struts2.json.JsonReturnBean;
import com.fare.taluo.queue.CreatePersistenceTask;
import com.fare.taluo.util.DateJsonValueProcessor;
import com.fare.taluo.util.XmlUtil;
import com.fare.taluo.vo.crawl.statistics.PersistentRecordeVO;
@Controller("persistenceOperteAction")
@Namespace("/")
public class PersistenceOperteAction extends BaseFdcAction{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	protected final Logger log=Logger.getLogger(getClass());
	@Resource(name ="persistentRecordeApplication")
	private PersistentRecordeApplication persistentRecordeApplication;
	
	@Inject
	private CreatePersistenceTask createPersistenceTask;
	
	private List<PersistentRecordeVO> listVo;
	
	private String data;
	
	//开始日期，结束日期，频率
	private String startDateStr;
	
	private String endDateStr;
	
	private Integer frequency;
	
	private Integer status;
	
	//页面跳转
	public String toOperteJsp(){
		listVo=persistentRecordeApplication.findAll();
		JsonConfig jsc = new JsonConfig();
		jsc.registerJsonValueProcessor(Date.class, new DateJsonValueProcessor("yyyy-MM-dd HH:mm:ss"));
		this.data=JSONArray.fromObject(this.listVo,jsc).toString();
		returnPageURL="/fare/crawlStatistics/_persistenceOperte.jsp";
		return "dispatcher";
	}
	
	public String toDoOperte() throws Exception{
		String result=null;
		JSONArray ja = new JSONArray();
		createPersistenceTask.setFlag(frequency);
		createPersistenceTask.setDate(startDateStr);
		createPersistenceTask.setEndflagDate(endDateStr);
		createPersistenceTask.setStatus(status);
		try {
			createPersistenceTask.execute();
			result="SUCCESS";
		} catch (Exception e) {
			result=e.getMessage();
		}
		ja.add(result);
		super.toJSONOut(ja.toString());
//		response.setContentType("text/html;charset=UTF-8"); 
//		response.getWriter().print(result);
//		return toOperteJsp();
		return null;
	}

	public List<PersistentRecordeVO> getListVo() {
		return listVo;
	}

	public void setListVo(List<PersistentRecordeVO> listVo) {
		this.listVo = listVo;
	}

	public String getData() {
		return data;
	}

	public void setData(String data) {
		this.data = data;
	}

	public String getStartDateStr() {
		return startDateStr;
	}

	public void setStartDateStr(String startDateStr) {
		this.startDateStr = startDateStr;
	}

	public String getEndDateStr() {
		return endDateStr;
	}

	public void setEndDateStr(String endDateStr) {
		this.endDateStr = endDateStr;
	}

	public Integer getFrequency() {
		return frequency;
	}

	public void setFrequency(Integer frequency) {
		this.frequency = frequency;
	}

	public Integer getStatus() {
		return status;
	}

	public void setStatus(Integer status) {
		this.status = status;
	}
	
}
